package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Contractor;
import com.init.engineering.pojo.Developer;
import com.init.engineering.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;


import java.util.List;

public interface ProjectDao {

    /**
     * 查找所有工程
     * 并查找该工程的开发商及承包商详情信息，实现一对一级联查询
     * @return
     */
    @Results(id = "projectMap", value = {
            @Result(column = "did", property = "did"),
            @Result(column = "cid", property = "cid"),
            @Result(column = "updatetime", property = "updatetime"),
            //EAGER:查询后立即加载，LAZY:只有当查询的数据被使用时，去查询加载
            @Result(column = "did", property = "developer",
                    javaType = Developer.class,
                    one = @One(select = "com.init.engineering.dao.DeveloperDao.findDevByDid",
                            fetchType = FetchType.EAGER)),  //一对一关联查询
            @Result(column = "cid", property = "contractor",
                    javaType = Contractor.class,
                    one = @One(select = "com.init.engineering.dao.ContractorDao.findConByCid",
                            fetchType = FetchType.EAGER)),
    })
    @Select("select * from project where status = 1")
    List<Project> findProjects();

    /**
     * 分页查询工程
     * 实现按条件查找的动态sql：
     * 若条件不为空，则带入条件进行查询，若条件为空，则查询所有工程
     * @param queryPageBean
     * @return
     */
    @ResultMap("projectMap")
    @Select({"<script> SELECT * FROM project <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and pname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Project> findProjectsByPage(QueryPageBean queryPageBean);


    /**
     * 查询符合分页查询的总条数
     * @param queryString
     * @return
     */
    @Select({"<script> SELECT count(*) FROM project <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and pname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);


    /**
     * 通过工程id查找工程
     * @param pid
     * @return
     */
    @Select("SELECT * FROM `project` where pid = #{id} and status = 1")
    Project findProjectByPid(int pid);

    /**
     * 逻辑删除工程
     * 执行更新操作，将属性status的值修改为0，表示将该数据删除
     * @param pid
     * @return
     */
    @Update("UPDATE project set status = 0 where pid = #{pid}")
    int updateStatusProject(@Param("pid")int pid);

    /**
     * 更新工程信息
     * 但不能更新status属性
     * @param project
     * @return
     */
    @Update("<script>update project <set><if test='pname != null'>pname = #{pname},</if>" +
            "<if test='starttime != null'>starttime = #{starttime},</if>" +
            "<if test='endtime != null'>endtime = #{endtime},</if>" +
            "<if test='did != null'>did = #{did},</if>" +
            "<if test='cid != null'>cid = #{cid},</if> " +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where pid = #{pid}</script>")
    int updateProject(Project project);

    /**
     * 新建一个工程
     * @param project
     * @return
     */
    @Insert("insert into project(pname,starttime,endtime,did,cid,updatetime,status)" +
            " values(#{pname},#{starttime},#{endtime},#{did},#{cid},#{updatetime},1)")
    int saveProject(Project project);

    /**
     * 通过开发商Did查找对应工程
     * 一个开发商可对应多个项目，通过开发商did查看该开发商开发的所有项目
     * @param did
     * @return
     */
    @Select("select * from project where did = #{did}")
    List<Project> findProjectsByDid(String did);

    /**
     * 通过承包商Cid查找对应工程
     * @param cid
     * @return
     */
    @Select("select * from project where cid = #{cid}")
    List<Project> findProjectsByCid(String cid);

}
